package com.wxx.宝石与石头;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author: Jetwang
 * @date: 2022/3/24 21:38
 * @description: Solution
 * 给你一个字符串 jewels 代表石头中宝石的类型，另有一个字符串 stones 代表你拥有的石头。 
 * stones 中每个字符代表了一种你拥有的石头的类型，你想知道你拥有的石头中有多少是宝石。
 * @version: 1.0
 */
public class Solution {
    public static int numJewelsInStones(String jewels, String stones) {
        Map<Character, Character> map = new HashMap<>();
        int count = 0;
        for (int i = 0; i < jewels.length(); i++) {
            map.put(jewels.charAt(i),jewels.charAt(i));
        }
        for (int i = 0; i < stones.length(); i++) {
            if (map.containsKey(stones.charAt(i))) {
                count++;
            }
        }
        return count;
    }

    /**
     * 使用Set集合
     * @param jewels
     * @param stones
     * @return
     */
    public static int numJewelsInStones02(String jewels, String stones) {
        Set<Character> set = new HashSet<>();
        int count = 0;
        for (int i = 0; i < jewels.length(); i++) {
            set.add(jewels.charAt(i));
        }
        for (int i = 0; i < stones.length(); i++) {
            if (set.contains(stones.charAt(i))) {
                count++;
            }
        }
        return count;
    }



    public static void main(String[] args) {
        int count01 = numJewelsInStones("z", "ZZ");
        System.out.println(count01);
        int count02 = numJewelsInStones("aA", "aAAbbbb");
        System.out.println(count02);
        int count03 = numJewelsInStones02("aA", "aAAbbbb");
        System.out.println(count03);
    }

}
